using System.Windows.Forms;
using Belmost4PDA.Fasade;

namespace Belmost4PDA.Logic
{
    public class Mapper_ComboBox:AbstractMapper
    {
        public Control_ComboBox comboBox;

        public Mapper_ComboBox(Control_ComboBox control)
        {
            comboBox = control;
            SetValName(((PanelItem)(control.GetFormControl().Tag)).Caption);
        }
        public override void ToControls(DataRows.DataRows row)
        {
            foreach (Control_ComboBox.ComboBoxItem o in ((ComboBox)(comboBox.GetFormControl())).Items)
            {
                if(o.ID==(row.FieldByNameDef(FieldName(), "")))
                {
                    SetInitValue(o.ID);
                    ((ComboBox) (comboBox.GetFormControl())).SelectedItem = o;
                    break;
                }               
            }
            // ((ComboBox)(comboBox.GetFormControl())).SelectedItem=(row.FieldByNameDef(FieldName(), "");
            
        }

        public override bool IsKey()
        {
           return false;
        }

        public override string FieldName()
        {
            return ((PanelItem)(comboBox.GetFormControl().Tag)).FieldName;
        }

        public override string FieldVal()
        {
            if (((ComboBox)(comboBox.GetFormControl())).SelectedItem != null)
                return ((Control_ComboBox.ComboBoxItem)(((ComboBox)(comboBox.GetFormControl())).SelectedItem)).ID;
            else
                return "";

        }

        public override bool TreatNull()
        {
            return true;
        }
    }
}
